﻿using Joker.Model;
using Joker.View.Editor;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Joker.View.Management {
    public partial class WarehouseOutManage : Form {
        public string sqlstr = "SELECT [WarehouseOut].[ID],[WarehouseOutType].[TypeName] [Type],[WarehouseOut].[Date],[WarehouseOut].[State],[Warehouse].[Name] [WarehouseName],[Goods].[Name] [GoodsName],[WarehouseOutDetail].[Amount],[Supply].[Name] [SupplyName] FROM [WarehouseOutDetail] JOIN [WarehouseOut] ON [WarehouseOut].[ID] = [WarehouseOutDetail].[WarehouseOutID] JOIN [WarehouseOutType] ON [WarehouseOut].[TypeID] = [WarehouseOutType].[ID] JOIN [Warehouse] ON [Warehouse].[ID] = [WarehouseOut].[WarehouseID] JOIN [Goods] ON [Goods].[ID] = [WarehouseOutDetail].[GoodsID] JOIN [Supply] ON [Supply].[ID] = [WarehouseOutDetail].[SupplyID]";
        public WarehouseOutManage() {
            InitializeComponent();
        }

        private void WarehouseOutManage_Load(object sender, EventArgs e) {
            comboBox1.DataSource = DBHelper.GetDataTable("SELECT * FROM [WarehouseOutType]");
            comboBox1.DisplayMember = "TypeName";
            comboBox1.ValueMember = "ID";
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlstr);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e) {
            panel5.Enabled = checkBox1.Checked;
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
            DataGridView dgv = (DataGridView)sender;
            if (dgv.CurrentRow == null || e.RowIndex < 0 || e.ColumnIndex < 0 || e.RowIndex > dgv.Rows.Count || e.ColumnIndex >= dgv.Rows[0].Cells.Count) {
                contextMenuStrip1.Items[1].Enabled = false;
                return;
            }
            if (e.Button.Equals(MouseButtons.Right)) {
                dgv.CurrentCell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
                contextMenuStrip1.Items[1].Enabled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            dataGridView1.DataSource = DBHelper.GetDataTable($"{sqlstr} where [WarehouseOutType].[ID] = {comboBox1.SelectedValue}{(checkBox1.Checked ? $" and [WarehouseOut].[Date] BETWEEN CAST('{dateTimePicker1.Value.ToString("yyyy-MM-dd")}' AS DATE) AND CAST('{dateTimePicker2.Value.ToString("yyyy-MM-dd")}' AS DATE)" : "")}");
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e) {
            WarehouseOutDataEdit form = new WarehouseOutDataEdit();
            OpenForm.NewForm(this,form,false);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) {
            WarehouseOutDataEdit form = new WarehouseOutDataEdit();
            form.WarehouseOutID = (int)dataGridView1.CurrentRow.Cells[0].Value;
            OpenForm.NewForm(this, form, false);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {
            int ID = (int)dataGridView1.CurrentRow.Cells[0].Value;
            if (MessageBox.Show($"确认删除ID为{ID}的所有数据吗？","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes) {
                bool result = true;
                result &= DBHelper.ExecuteNonQuery($"DELETE [WarehouseOutDetail] WHERE [WarehouseOutID] = {ID}");
                result &= DBHelper.ExecuteNonQuery($"DELETE [WarehouseOut] WHERE [ID] = {ID}");
                if (result) {
                    MessageBox.Show("删除成功!");
                } else {
                    MessageBox.Show("删除失败!");
                }
            }
        }
    }
}
